import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.nio.IntBuffer;
import java.sql.SQLException;

import org.json.JSONException;

/**
 * 
 * @author Paul Peasegood
 * Used to access the game though the command line can used for testing/debuging and game overrides
 *
 */
public class CommandlineInterface {

	/**
	 * prints out list of commands
	 */
	private static void help()
	{
		System.out.println("attack: attack using current weapon");
		System.out.println("connect: (IP) (port) connects player to DM server");
		System.out.println("display: display player stats");
		System.out.println("exit: exits program");
    	System.out.println("endTurn: end player turn");
		System.out.println("heal: [HP] restored HP");
		System.out.println("loadPlayer: [name] loads a player from the database");
		System.out.println("move: [distance] reduces action points for movement");
		System.out.println("newWeapon: [toHit] [dice number] [dice sides] [dam bounus] (useCost)");
		System.out.println("takeHit: [toHit] [damage] takes  damage from an attack");
		System.out.println("send [message] sends message to DM");
		System.out.println("send [target] sends an attack with a target to DM");
		System.out.println("setHP: [HP] sets HP to set amount");
	}

	/**
	 * @param args
	 * @throws IOException 
	 * @throws SQLException 
	 * @throws JSONException 
	 * @throws NumberFormatException 
	 */
	public static void main(String[] args) throws IOException, SQLException, NumberFormatException, JSONException
	{
		Player player = new Player();
		String input;
		String[] sections;
		System.out.println("welcome to RPG");
		while(true)
		{
			System.out.println("enter an command, type help to for list of commands");
			BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
            input = br.readLine();
            
            sections = input.split(" ");
            /* loop used for debuging
            for(int i = 0;i < sections.length; i++)
            {
                    System.out.println(i + ": " + sections[i]);
            }
            */
            switch (sections[0])
            {
            
            case "help":
            	help();
            	break;
            case "connect":
            	if(sections.length == 1)
            	{
            		player.connect("localhost", 8000);
            	}
            	else if (sections.length == 3)
            	{
            		player.connect(sections[1], Integer.parseInt(sections[2]));
            	}
            	else { System.out.println("incorrect parameters"); }
            	break;
            case "send":
            	player.sendMessage(sections[1]);
            	break;
            case "sendAttack":
            	player.sendAttack(Integer.parseInt(sections[1]));
            	break;
            case "attack":
            	player.attack();
            	break;
            case "display":
            	System.out.println(player.toString());
            	break;
            case "takeHit":
            	player.takeHit(Integer.parseInt(sections[1]),
            			Integer.parseInt(sections[2]));
            	break;
            case "posion":
            	player.addDOT(Integer.parseInt(sections[1]), 
            			Double.parseDouble(sections[2]));
            	break;
            case "move":
            	player.move(Integer.parseInt(sections[1])); 
            	break;
            case "loadPlayer":
            	player = Database.loadPlayer(sections[1]);
            	System.out.println(player.toString());
            	break;
            case "loadWeapon":
            	player.setWeapon(Database.loadWeapon(sections[1]));
            	System.out.println(player.toString());
            	break;
            case "loadPosionWeapon":
            	player.setWeapon(Database.loadPosionWeapon(sections[1]));
            	System.out.println(player.toString());
            	break;
            case "loadArmor":
            	player.setArmor(Database.loadArmor(sections[1]));
            	System.out.println(player.toString());
            	break;
            case "heal":
            	player.heal(Integer.parseInt(sections[1]));
            	break;
            case "setHP":
            	player.setHP(Integer.parseInt(sections[1]));
            	break;
            case "setAP":
            	player.setAP(Integer.parseInt(sections[1]));
            	break;
            case "newWeapon":
            	if(sections.length == 5)
            	{
            		player.setWeapon(new Weapon(Integer.parseInt(sections[1]),
            				Integer.parseInt(sections[2]),
            				Integer.parseInt(sections[3]),
            				Integer.parseInt(sections[4])));
            	}
            	else if(sections.length == 7)
            	{
            		player.setWeapon(new Weapon(Integer.parseInt(sections[1]),
            				Integer.parseInt(sections[2]),
            				Integer.parseInt(sections[3]),
            				Integer.parseInt(sections[4]),
            				Integer.parseInt(sections[5]),
            				Integer.parseInt(sections[6]),0));
            	}
            	else
            	{
            		System.out.println("invaild paramters");
            	}
            	break;
            case "newPosionWeapon":
            	player.setWeapon(new PosionWeapon());
            	break;
            case "newArmor":
            	player.setArmor(new Armor (Integer.parseInt(sections[1]),
            				Integer.parseInt(sections[2]),
            				Integer.parseInt(sections[3])));
            	break;
            case "endTurn":
            	player.endTurn();
            	break;
            case "exit":
            	player.disconnect();
            	System.exit(0);
            default:
            	System.out.println("invaild command");            		
            }

		}
	}

}
